<?php
//首页调用
namespace NewAdmin\Controller;
use Think\Controller;
class UsersController extends CommonController {
    //会员列表
    public function index(){
    	$where=array();
    	if($_GET['phone']){
    		$where['phone']=$_GET['phone'];
    	}
        $users   =   D('Users');
        $count      = $users->where($where)->count();
	    $this->assign('count',$count);
	    $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数
	    $Page->setConfig('next','下一页');
	    $Page->setConfig('prev','上一页');
	    $show       = $Page->show();// 分页显示输出
  		$users_list=$users
  				->where($where)
  				->limit($Page->firstRow.','.$Page->listRows)
  				->order('id desc')
  				->select();
  		$this->assign('page',$show);// 赋值分页输出		
  		$this->assign('users_list',$users_list);
        $this->assign('title','会员列表');
		$this->display();
    }

	function check_username(){
		$phone=I('get.username');
		$users=M('users');
		$rs=$users->where('phone='.$phone)->find();
		if($rs){
			$info['code']=1;
			$this->ajaxReturn($info);
		}else{
			$info['code']=0;
			$this->ajaxReturn($info);
		}
	}
	function check_username_admin(){
		$phone=I('get.username');
		$user=M('user');
		$rs=$user->where('user_name='.$phone)->find();
		if($rs){
			$info['code']=1;
			$this->ajaxReturn($info);
		}else{
			$info['code']=0;
			$this->ajaxReturn($info);
		}
	}
	
	function addmember(){
		$data=I('post.');
		$users=M('users');
		$info['username']=$data['nickname']?$data['nickname']:$data['username'];
		$info['password']=passport_encrypt($data['password'],'wnsc');
		$info['ishidden']=$data['status'];
		$info['phone']=$data['username'];
		$info['addtime']=time();
		$info['sex']=$data['sex'];
		$rs=$users->where('phone='.$data['username'])->find();
		if($rs){
			$info['code']=2;
			$this->ajaxReturn($info);
		}else{
			$rs=$users->add($info);
			if($rs){
				$info['code']=1;
				$this->ajaxReturn($info);
			}else{
				$info['code']=0;
				$this->ajaxReturn($info);
			}
		}
		
	}
	function addmember_admin(){
		$data=I('post.');
		$user=M('user');
		$info['user_name']=$data['user_name'];
		$info['password']=md5($data['password'].'ltj2016');
		$info['nickname']=$data['nickname']?$data['nickname']:$data['user_name'];
		$info['ishidden']=$data['ishidden'];
		$info['user_level']=$data['user_level'];
		$info['addtime']=time();
		
	
		$rs=$user->where('user_name='.$data['user_name'])->find();
		if($rs){
			$info['code']=2;
			$this->ajaxReturn($info);
		}else{
			$rs=$user->add($info);
			if($rs){
				$info['code']=1;
				$this->ajaxReturn($info);
			}else{
				$info['code']=0;
				$this->ajaxReturn($info);
			}
		}
		
	}
	
	
	function getmemberdetail(){
		$data=I('post.');
		$users=M('users');
		$id=$data['uid'];
		$rs=$users->find($id);
		$this->ajaxReturn($rs);
	}
	
	function updatemember(){
		$data=I('post.');
		$users=M('users');
		$rs=$users->save($data);
		if($rs){
			$info['code']=1;
			$this->ajaxReturn($info);
		}else{
			$info['code']=0;
			$this->ajaxReturn($info);
		}
	}
	
	function addmemberaccount(){
		$data=I('post.');
		$users=M('users');
		$users_log=M('users_log');
		$info['id']=$data['id'];
		$rs=$users->find($data['id']);
		$info['money']=$rs['money']+$data['num'];
		$rs=$users->save($info);
		
		$log['uid']=$data['id'];
		$log['addtime']=time();
		$log['money']=abs($data['num']);
		if($data['num']<0){
			$log['type']=0;
		}else{
			$log['type']=1;
		}
		
		$log['other']=$data['text'];
		$log['money_type']=3;
		$users_log->add($log);
		
		if($rs){
			$info['code']=1;
			$this->ajaxReturn($info);
		}else{
			$info['code']=0;
			$this->ajaxReturn($info);
		}
	}
	
	
	function updatememberpassword(){
		$data=I('post.');
		$users=M('users');
		$info['id']=$data['uid'];
		$info['password']=passport_encrypt($data['user_password'],'wnsc');
		$rs=$users->save($info);
		if($rs){
			$info['code']=1;
			$this->ajaxReturn($info);
		}else{
			$info['code']=0;
			$this->ajaxReturn($info);
		}
	}
	function updatememberpassword_admin(){
		$data=I('post.');
		$user=M('user');
		$info['id']=$data['uid'];
		$info['password']=md5($data['user_password'].'ltj2016');
		
		$rs=$user->save($info);
		if($rs){
			$info['code']=1;
			$this->ajaxReturn($info);
		}else{
			$info['code']=0;
			$this->ajaxReturn($info);
		}
	}
	
	function accountdetail(){
		$id=I('get.id');
		$users_log=M('users_log');
		$users=M('users');
		$user_info=$users->find($id);
		$count      = $users_log->where('uid='.$id)->count();
	    $this->assign('count',$count);
	    
	    $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数
	    $Page->setConfig('next','下一页');
	    $Page->setConfig('prev','上一页');
	    $show       = $Page->show();// 分页显示输出
  		$users_list=$users_log
  				->where('uid='.$id)
  				->limit($Page->firstRow.','.$Page->listRows)
  				->order('id desc')
  				->select();
  		$this->assign('page',$show);// 赋值分页输出		
  		$this->assign('users_list',$users_list);
  		$this->assign('user_info',$user_info);
  		
        $this->assign('title','会员积分列表');
		$this->display();
		
	}
	
	function admin_index(){
		$user   =   D('User');
        $count      = $user->count();
	    $this->assign('count',$count);
	    
	    $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数
	    $Page->setConfig('next','下一页');
	    $Page->setConfig('prev','上一页');
	    $show       = $Page->show();// 分页显示输出
  		$user_list=$user
  				->limit($Page->firstRow.','.$Page->listRows)
  				->order('id desc')
  				->select();
  		$this->assign('page',$show);// 赋值分页输出		
  		$this->assign('user_list',$user_list);
        $this->assign('title','会员列表');
		$this->display();

	}
	function angel(){
        $user   =   D('users');
        $id=I('get.id');
        $user_info=$user->find($id);
        $this->assign('user_info',$user_info);
        $angel=M('angel');
        $angel_list=$angel->where('uid='.$id)->select();
      
        $this->assign('angel_list',$angel_list);
        $this->assign('title',' 天使代言人');
        $this->display();
    }
    function adddaiyan(){
	    $data=I('post.');
	    $info['uid']=$data['uid'];
	    $info['type_id']=$data['type_id'];
	    $info['type']=$data['daiyan_type'];
	    $angel=M('angel');
	    $rs=$angel->add($info);
	    if($rs){
	        $this->ajaxReturn(1);
        }else{
	        $this->ajaxReturn(0);
        }
    }

    function  del_angel(){
        $id=I('get.id');
        $uid=I('get.uid');
        $angel=M('angel');
        $angel->delete($id);
        $this->redirect('users/angel', array('id' => $uid));

    }

    function  editdaiyan(){
        $data=I('post.');
        $info['id']=$data['id_edit'];
        $info['type_id']=$data['type_id_edit'];
        $info['type']=$data['daiyan_type_edit'];
        $angel=M('angel');
        $rs=$angel->save($info);
        if($rs){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(1);
        }

    }
    
    function tj_user(){
    	$id=I('get.id');
    	$user   =   D('users');
        $count      = $user->where('tj_userid='.$id)->count();
	    $this->assign('count',$count);
	    
	    $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数
	    $Page->setConfig('next','下一页');
	    $Page->setConfig('prev','上一页');
	    $show       = $Page->show();// 分页显示输出
  		$user_list=$user
  				->where('tj_userid='.$id)
  				->limit($Page->firstRow.','.$Page->listRows)
  				->order('id desc')
  				->select();
  		$this->assign('page',$show);// 赋值分页输出		
  		$this->assign('user_list',$user_list);
        $this->assign('title','推荐会员列表');
		$this->display();
  				
  	
    }

    function  city_partner(){
        $count   = M('partner')->count();
        $Page       = new \Think\Page($count,15);// 实例化分页类 传入总记录数
        $Page->setConfig('next','下一页');
        $Page->setConfig('prev','上一页');
        $show       = $Page->show();// 分页显示输出
        $users_list=M('partner')
            ->field('yt_partner.*,yt_users.username,yt_users.phone')
           ->join('yt_users on yt_partner.u_id=yt_users.id','left')
            ->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('page',$show);// 赋值分页输出
        $this->assign('count',$count);
        $this->assign('users_list',$users_list);
        $this->assign('title','城市合伙人管理');
        $this->display();
    }

    function  city_partner_edit(){
        if($_POST){
            $data=I('post.');
            if($data['id']){
                M('partner')->save($data);
                $this->ajaxReturn(2);
            }else{
                $rs_p=M('partner')->where('u_id='.$data['u_id'])->find();
                if($rs_p==null){
                    $rs=M('partner')->add($data);
                    if($rs){
                        $this->ajaxReturn(1);
                    }else{
                        $this->ajaxReturn(0);
                    }
                }else{
                    $this->ajaxReturn(4);
                }

            }
        }else{
            if($_GET['id']){
                $rs_p=M('partner')->find($_GET['id']);
                $this->assign('rs_p',$rs_p);
                $this->assign('title','编辑城市合伙人');
            }else{
                $this->assign('title','添加城市合伙人');
            }
            $this->display();
        }

    }

    function  area_angel(){
        $count=M('area_angel')
            ->field('yt_users.phone,yt_area_angel.*')
            ->join('yt_users on yt_users.id=yt_area_angel.uid','left')
            ->count();

        $Page       = new \Think\Page($count,15);// 实例化分页类 传入总记录数
        $Page->setConfig('next','下一页');
        $Page->setConfig('prev','上一页');
        $show       = $Page->show();// 分页显示输出
        $users_list=M('area_angel')
            ->field('yt_users.phone,yt_area_angel.*')
            ->join('yt_users on yt_users.id=yt_area_angel.uid','left')
            ->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('count',$count);
        $this->assign('list',$users_list);
        $this->assign('title','区域代言人管理');
        $this->display();
    }

    function edit_angel_status(){
        if($_POST){

            $data=I('post.');
            $info['id']=$data['id'];
            $info['status']=$data['data'];
            if($data['data']==-1){
                $info['jujue_id']=0;
            }
            $rs=M('area_angel')->save($info);
            if($rs){
                $this->ajaxReturn(1);
            }else{
                $this->ajaxReturn(0);
            }
        }
    }


    function authority(){
        $menu   =   D('Menu');
        $this->assign('title','权限配置');
        $menudatashowArray =   $menu->where('type = 0')->order('sort ASC')->select();
        $new_data=getTree($menudatashowArray,0);
        $this->assign('new_data',$new_data);
        $id=I('get.id');
        $where['uid']=$id;
        $where['type']=1;
        $rs=M('rbac')->where($where)->find();
        $this->assign('rs',$rs);
        $this->display();
    }

    function  edit_rbac(){
        if($_POST){
            $data=I('post.');
            $where['uid']=$data['uid'];
            $where['type']=1;
            $rs=M('rbac')->where($where)->find();
            if($rs){
                $eidt_where['bids']=$data['adIds'];
                $eidt_where['id']=$rs['id'];

                $result=M('rbac')->save($eidt_where);
            }else{
                $where['bids']=$data['adIds'];
                $result=M('rbac')->add($where);
            }
            if($result){
                $this->ajaxReturn(1);
            }else{
                $this->ajaxReturn(0);
            }
        }
    }
   
}
